<?php require_once('common.php');
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Koki extends Common {
	function __construct() {
		parent::__construct("koki");
		$this->load->model(array('pesanan', 'paket_makanan'));
	}

	public function index(){	
		$this->load->model('user');
		$pesanan = $this->pesanan->get(array('status != ' => STATUS_SUDAH_DIBAYAR), 'koki')->result_array();
		for($i = 0; $i < count($pesanan); $i++){
			$pesanan[$i]['detail'] = $this->pesanan->get_detail_pesanan($pesanan[$i]['id_pesanan']);
			$petugas = $this->user->get(array('id_petugas' => $pesanan[$i]['id_petugas']))->row_array();
			$pesanan[$i]['pelayan'] = $petugas['nama'];
		}
		$data['pesanan'] = $pesanan;
		$data['status'] = unserialize(STATUS_PESANAN);
		$data['pesan'] = $this->session->flashdata('pesan');
		$this->load->view('koki/landing_page', $data);
	}
	
	public function ubah_status($id_pesanan, $status_baru){
		if($this->pesanan->edit($id_pesanan, array('status'=>$status_baru))){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Status Pesanan berhasil diubah."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Status Pesanan gagal diubah."));
		}
		redirect(base_url()."index.php/koki");
	}
	
	public function paket_makanan(){
		$this->load->model('paket_makanan');
		$data['paket'] = $this->paket_makanan->get()->result_array();
		$data['pesan'] = $this->session->flashdata('pesan');
		$this->load->view('koki/list_paket', $data);
	}
	
	function add_paket(){
		$this->load->model('bahan_baku');
		$data['mode'] = 'ADD';
		$data['bahan'] = $this->bahan_baku->get()->result_array();
		$this->load->view('koki/form_paket',$data);
	}

	function edit_paket($id){
		$this->load->model('bahan_baku');
		$data['mode'] = 'EDIT';
		$data['detail'] = $this->paket_makanan->get(array('id_paket' => $id))->row_array();
		$data['bahan_paket'] = $this->paket_makanan->get_bahan_baku_paket(array('id_paket' => $id))->result_array();
		$data['bahan'] = $this->bahan_baku->get()->result_array();
		$this->load->view('koki/form_paket',$data);	
	}
	
	function save_paket(){
		$this->layout = FALSE;
		$mode = $this->input->post('mode');
		$data = array('nama' 		=> $this->input->post('nama'),
					  'harga'	=> $this->input->post('harga'),
					  'deskripsi' => $this->input->post('deskripsi'),
					  'id_petugas' => $this->user_data['id_petugas']);
		
		if($mode == 'ADD'){
			$id = $this->paket_makanan->add($data);
			$success = $id;
		}else{
			$id = $this->input->post('id_paket');
			$success = $this->paket_makanan->edit($id, $data);
		}
		
		//bahan baku paket makanan
		$bahan = $this->input->post('bahan_baku');
		$ukuran = $this->input->post('ukuran');
		if($bahan != NULL){
			if($mode == 'EDIT'){
				$this->paket_makanan->delete_bahan_baku_paket($id); //delete first before adding again
			}
			
			for($j = 0; $j < count($bahan); $j++){
				if($bahan[$j] != ""){
					$inp_paket = array('id_paket' => $id,
									   'id_bahan_baku' => $bahan[$j],
									   'ukuran' => $ukuran[$j]);
					$this->paket_makanan->add_bahan_baku_paket($inp_paket);
				}
			}
		}
		
		if($success){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Data Paket Makanan berhasil disimpan."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Data Paket Makanan gagal disimpan."));
		}
		redirect(base_url()."index.php/koki/paket_makanan");
	}
	
	function delete_paket($id){
		if($this->paket_makanan->delete($id)){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Paket Makanan berhasil dihapus."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Paket Makanan gagal dihapus."));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
	
}